﻿
layui.use(['form', 'layer', 'jquery'], function () {
    var form = layui.form;
    var layer = layui.layer;
    var $ = layui.jquery;
    var token = localStorage.getItem("token");
    console.log(token)
    $.ajax({
        url: '../User/MenuPidDo',
        type: "get",
        data: { token: token },
        success: function (res) {
            res = JSON.parse(res);
            console.log(res)
            var selectData = res.data;
            var selectHtml = '<option value="">顶级菜单</option>';
            selectData.forEach(function (item) {
                selectHtml += '<option value="' + item.id + '">' + item.menu_name + '</option>';
            });
            $('#dynamicSelect').html(selectHtml);
            form.render('select');
            form.on('select(dynamicFilter)', function (data) {
                console.log(data.value);
            });
        }
    });
    //    debugger;
    form.on('radio(showInfo)', function (data) {
        if (data.value == "2") {
            // 如果选中的是“按钮”，则显示隐藏的元素
            $('#textBox').show();
        } else {
            // 否则，隐藏元素
            $('#textBox').hide();
        }
    });

    //   debugger;
    // 重新渲染表单，确保 Layui 样式生效
    form.render();

    // 监听单选框的变化
    $('input[type=radio][name=type]').change(function () {
        var element = document.getElementById('AnNiu');
        if (element.style.display === 'none' || element.style.display === '') {
            element.style.display = 'block';
        } else {
            element.style.display = 'none';
        }
        // 如果您使用的是 Layui，可能需要重新渲染表单
        layui.form.render();
        if (this.value == '2') {
            $('#Jia').show();
            $('#AnNiu').show();
            form.render();
        } else {
            $('#Jia').hide();
            $('#AnNiu').hide();
            form.render();
        }

    });

    window.New = function New() {
        var NemBox = `<label class="layui-form-label" style="width: 100px;">功能名称:</label>
                                    <div class="layui-input-inline layui-input-wrap">
                                        <input type="text" id="permission_name" lay-verify="required" autocomplete="off" lay-affix="clear" class="layui-input">
                                    </div>
                                    <label class="layui-form-label" style="width: 100px;">功能路径:</label>
                                    <div class="layui-input-inline layui-input-wrap">
                                        <input type="text" id="is_perms" lay-verify="required" autocomplete="off" lay-affix="clear" class="layui-input">
                                    </div>
                                <div id="Jia" class="layui-input-block" style="display:none;">
                                    <input type="button" class="layui-btn" value="+"  onclick="New()">
                                </div>`
        $('#NemBox').append(NemBox);
        form.render();

    }


    layui.form.render();



    window.Enroll = function Enroll() {
        var parent_id = $('#dynamicSelect').val();
        var menu_name = $('#menu_name').val();
        var menu_url = $('#menu_url').val();
        var perms = $('#perms').val();
        var type = $('input[name="type"]:checked').val();
        var menu_icon = $('#menu_icon').val();
        var order_num = $('#order_num').val();
        var permission_name = "";
        var button = []; // 在全局作用域中初始化 button 数组
        if (type == 2) {
            $('#AnNiu').find('input[type="text"]').each(function (index, item) {
                if (index % 2 == 0) {
                    permission_name = $(item).val();
                } else {
                    button.push({
                        permission_name: permission_name,
                        is_perms: $(item).val()
                    });
                }
            });

        }
        var button = button;
        console.log(button)
        var token = localStorage.getItem("token");
        var model = JSON.stringify({
            menu_name: menu_name,
            menu_url: menu_url,
            perms: perms,
            type: new Number(type),
            menu_icon: menu_icon,
            order_num: new Number(order_num),
            parent_id: new Number(parent_id),
            button: button
        });
        console.log(model)
        console.log(token)
        $.ajax({
            url: '../User/MenuAddDo',
            type: "post",
            data: { model: model, token: token },
            success: function (res) {
                console.log(res)
                var ressult = JSON.parse(res)
                if (ressult.code == 200) {
                    alert(ressult.message)
                    window.location.href = "../User/Menu"
                }
                else {
                    alert(ressult.message)
                }
            }

        });





        window.showInfo = function showInfo() {
            console.log("按钮")
        }




    }





});
